package com.demo.example.hot100;

import java.util.ArrayList;
import java.util.List;

public class TelephoneConsist {
    public static void main(String[] args) {
        String digits="23";
        letterCombinations(digits);
    }
    public static List<String> letterCombinations(String digits){
        if(digits==null || digits.length()==0){
            return new ArrayList<>();
        }
        List<String> res=new ArrayList<>();
        char[][] map=new char[][]{
                {'a','b','c'}, //0~2
                {'d','e','f'}, //1~3
                {'g','h','i'}, //2~4
                {'j','k','l'},
                {'m','n','o'},
                {'p','q','r','s'},
                {'t','u','v'},
                {'w','x','y','z'}
        };
        //先来一个for循环进行digits的遍历，来进行取数
        //要确认digits并不止一个数字才能进行以下的组合操作
        if(digits.length()==1){
            for(int i=0;i<map[digits.charAt(0)-50].length;i++){
                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append(map[digits.charAt(0)-50][i]);
                res.add(stringBuilder.toString());
            }
            return res;
        }
        //此时不止一个数字了，进行组合操作
            int i=0;
            char next=digits.charAt(i+1);
            char pre=digits.charAt(i);
            //先进行两个数字的模拟拟合
            //也可以考虑傻瓜做法，反正最多就4个数字，可以多来几个二重循环
            List<String> tmp1=new ArrayList<>();
        for(int j=0;j<map[pre-50].length;j++){
            StringBuilder stringBuilder=new StringBuilder();
            stringBuilder.append(map[digits.charAt(0)-50][j]);
            tmp1.add(stringBuilder.toString());
        }
        List<String> tmp2=new ArrayList<>();
        //存储第二步的结果
            for(int j=0;j<tmp1.size();j++){
                for(int k=0;k<map[next-50].length;k++){
                    StringBuilder stringBuilder=new StringBuilder();
                    stringBuilder.append(tmp1.get(j));
                    stringBuilder.append(map[next-50][k]);
                    tmp2.add(stringBuilder.toString());
                }
            }
            //看看是否为三个数字，进行一个判断
        if(i+2<digits.length()){
            i++;
            next=digits.charAt(i+1); //更新新的号码
            List<String> tmp3=new ArrayList<>();
            //存储第三步的结果
            for(int j=0;j<tmp2.size();j++){
                for(int k=0;k<map[next-50].length;k++){
                    StringBuilder stringBuilder=new StringBuilder();
                    stringBuilder.append(tmp2.get(j));
                    stringBuilder.append(map[next-50][k]);
                    tmp3.add(stringBuilder.toString());
                }
            }
            //再做判断
            if(i+2<digits.length()){
                i++;
                next=digits.charAt(i+1); //更新新的号码
                List<String> tmp4=new ArrayList<>();
                for(int j=0;j<tmp3.size();j++){
                    for(int k=0;k<map[next-50].length;k++){
                        StringBuilder stringBuilder=new StringBuilder();
                        stringBuilder.append(tmp3.get(j));
                        stringBuilder.append(map[next-50][k]);
                        tmp4.add(stringBuilder.toString());
                    }
                }
                return tmp4;
            }else{
                return tmp3;
            }
        }else{
            return tmp2;
        }
    }
}
